package com.wanshu.controller;


import com.alibaba.fastjson.JSON;
import com.wanshu.entity.Order;
import com.wanshu.entity.vo.ShopOrder;
import com.wanshu.service.OrderService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin("http://localhost:8080")
@RequestMapping("/order")
public class OrderController {

    @Resource
    OrderService orderService;

    @RequestMapping("/insert")
    public String insert(@RequestBody Order order) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String orderTime = simpleDateFormat.format(date);
        order.setOrderTime(orderTime);
        Integer insert = orderService.insert(order);
        String message = "添加失败";
        if (insert > 0) {
            message = "添加成功";
        }
        return message;
    }

    @RequestMapping("/selectByUserId")
    public String selectByUserId(@RequestBody Order order){
        List<ShopOrder> select = orderService.selectByUserId(order.getUserId());
        return JSON.toJSONString(select);
    }
}
